#!/bin/bash
#===============================================================================
#
#          FILE:  AKM-terminal
# 
#         USAGE:  sudo ./AKM-terminal 
# 
#   DESCRIPTION:  Automatically un-tars, compiles, and installs a kernel
# 
#  REQUIREMENTS:  See README
#        AUTHOR:  Anthony Byrne, abyrne561@gmail.com
#       VERSION:  1.2
#       CREATED:  09/21/2009 05:05:49 PM EDT
#       LICENSE:  GPL
#===============================================================================
################################################################################
# Auto Linux Kernel Maker (AKM) 
# Copyright 2009 Anthony Byrne
#
# This file is part of AKM
#
# AKM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AKM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AKM.  If not, see <http://www.gnu.org/licenses/>.
################################################################################
Err="\E[31;47m"
Y="\E[33;40m"
E="\033[0m"

# Text color variables
#  Output Colors
ERR=$(tput bold && tput setaf 1 && tput setb 7)
WARN=$(tput bold && tput setaf 4 && tput setb 7)
OP=$(tput bold && tput setaf 0 && tput setb 7)
#  Extra Colors (Not used but kept for future)
BLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YLW=$(tput setaf 3)
BLUE=$(tput setaf 4)
PURPLE=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
ENDCLR=$(tput sgr0)

# Make sure your in the folder where a .tar.bz2 downloaded from kernel.org is
# MAKE SURE YOUR ROOT WHEN RUNNING THIS!

# Show error if not root
if [ $USER != root ]; then
  echo "${ERR}ERROR: Must Be Root${ENDCLR}"
  exit 0
fi

( # Start LOG
# Warning
echo ""
echo "${WARN}Make sure that you launched this program from a directory which has a .tar.bz2 downloaded from kernel.org. If you didn't do this right, KILL this program, don't just hit cancel!${ENDCLR}"

echo ""

# Ask and fill variables
echo "${OP}What is the version of the kernel you are trying to install? (e.g 2.6.0) MAKE SURE THIS IS RIGHT!${ENDCLR}"
read KERNEL_VER
echo $KERNEL_VER

# Decompress
echo "${OP}Decompressing Kernel...${ENDCLR}"
tar xivf linux-${KERNEL_VER}.tar.bz2

# Go into Kernel source
cd linux-$KERNEL_VER

# Choose a Method
echo ""
echo ""
echo "${OP}Choose a method for configuration${ENDCLR}"
echo ""
echo "${OP}1) Configure using the GUI (requires qt3-examples)${ENDCLR}" 
echo "${OP}2) Configure using the Terminal${ENDCLR}"
echo "${OP}3) Use a pre-existing config file${ENDCLR}"
echo "${OP}Enter the number of your choice${ENDCLR}"
read METHOD
echo $METHOD

if [[ "$METHOD" == "1" ]]; then
make xconfig  
fi

if [[ "$METHOD" == "2" ]]; then
xterm -e make config  
fi

if [[ "$METHOD" == "3" ]]; then
echo -e "${WARN}You've set this script to use a preconfigured kernel config file. Put that config file in the linux-$KERNEL_VER directory that was just created. Make sure that file is named .config${ENDCLR}"

read -n1 -r -p "Press any key to continue..." key
xterm -e make oldconfig  
fi


# Compile the kernel (takes a long time)
echo "${OP}Compiling the Kernel (takes a long time)${ENDCLR}"
make 

# Install the kernel modules
echo "${OP}Installing Kernel Modules${ENDCLR}"
make modules_install 


# Install the kernel
echo "${OP}Installing Kernel${ENDCLR}"
make install 


# Crestes a new Initial RAM File System (initramfs)
echo "${OP}Running Post-Installation Tasks (Generating INITRAMFS)${ENDCLR}" 
update-initramfs -k ${KERNEL_VER} -c


# Updates Bootloader
echo "${OP}Running Post-Installation Tasks (Updating GRUB)${ENDCLR}"
update-grub


cd ..

echo "${OP}Do you want to clean up?${ENDCLR}" 
echo ""
echo "${OP}1) No, Leave everything.${ENDCLR}"
echo "${OP}2) Yes, just delete the un-needed files for me.${ENDCLR}"
echo "${OP}3) Yes, but keep the .config file for the future.${ENDCLR}" # see if statement
echo "${OP}Enter the number of your choice${ENDCLR}"
read DELETE
echo $DELETE

if [[ "$DELETE" == "2" ]]; then
rm -r -f linux-$KERNEL_VER
fi

if [[ "$DELETE" == "3" ]]; then
cp /boot/config-${KERNEL_VER} -t ~/
rm -r -f linux-$KERNEL_VER
echo "${OP}If you want to use the config file from this kernel in the furture, it is located in your home folder under the name of config-${KERNEL_VER}${ENDCLR}"
sleep 2
fi

# ALL DONE
echo "${WARN}All Done! Your new kernel has been installed!${ENDCLR}"


) 2>&1 | tee AKM.log
